﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExecuteScalar
{
    class Program
    {
        static void Main(string[] args)
        {
            //链接字符串
            const string conStr = "Data Source=.;Initial Catalog=ADO_NET_Simple;Integrated Security=True";
            //连接通道
            SqlConnection conn = new SqlConnection(conStr);
            //打开链接
            conn.Open();
            //命令对象
            SqlCommand comm = new SqlCommand();
            //指定链接通道
            comm.Connection = conn;
            //即将执行的sql命令
            comm.CommandText = "select count(*) from Students";
            //提交命令到数据库
            //ExecuteScalar()	执行查询，返回结果集的首行首列
            object result = comm.ExecuteScalar();
            //关闭链接
            conn.Close();
            Console.WriteLine("总共有{0}条数据", result);
            Console.ReadKey();
        }
    }
}
